package com.sun.dream.fish.util;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.mapstruct.factory.Mappers;

import java.util.List;

/**
 * Description:
 * Created By ljl on 2023/12/20.
 */
public class PageUtil {
    public static <T, R, B> Page<R> pageToVo(Page<T> newPage, Class<B> basic, String methodName) {
        Page<R> resultPage = new Page<>();
        resultPage.setCurrent(newPage.getCurrent());
        resultPage.setSize(newPage.getSize());
        resultPage.setPages(newPage.getPages());
        resultPage.setTotal(newPage.getTotal());
        Class<?> aClass = Mappers.getMapper(basic).getClass();
        try {
            resultPage.setRecords((List<R>) aClass.getDeclaredMethod(methodName, List.class).invoke(aClass.getDeclaredConstructor().newInstance(), newPage.getRecords()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultPage;
    }

    public static <T, R, B> Page<R> pageToVo(Page<T> newPage, Class<B> basic) {
        return pageToVo(newPage, basic, "entityToVO");
    }
}
